﻿package view.mainUI.mainBottom.component
{
    import com.adobe.utils.*;
    import com.core.*;
    import com.core.display.*;
    import com.sh.game.consts.*;
    import com.sh.game.display.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import morn.core.components.*;
    import pluginfw.*;
    import view.appDrag.*;
    import view.uilt.*;

    public class MainSkillItem extends Sprite
    {
        private var mImgBg:Image;
        private var mSkillImg:Image;
        private var mTextNum:TextField;
        private var mImgLabel:Image;
        private var mTextSC:TextField;
        private var mSkillInfo:Object;
        private var mData:Object;
        private var mShortcut:String;
        private var mType:int = 0;
        private var mID:int = 0;
        private var mCDTime:Number = 0;
        private var mStartCDTime:int = 0;
        private var mShowingCD:Boolean = false;
        private const SKILL_TYPE:int = 1;
        private const ITEM_TYPE:int = 2;
        private var mWedgeSprite:Sprite;
        private var mMaskSprite:Sprite;
        private var mItemMask:Sprite;
        private var mSkillMC:XMLMovieClip;
        private var mAutoSkillMC:XMLMovieClip;
        private var timeoutID:uint;
        private var mIntervalID:uint = 0;
        private const SHORTObj:Object;
        private var mClickTime:int;
        public static const effectXml:Class = MainSkillItem_effectXml;
        public static const effect2Xml:Class = MainSkillItem_effect2Xml;

        public function MainSkillItem()
        {
            this.SHORTObj = {1:Keyboard.NUMBER_1, 2:Keyboard.NUMBER_2, 3:Keyboard.NUMBER_3, 4:Keyboard.NUMBER_4, 5:Keyboard.NUMBER_5, Q:Keyboard.Q, W:Keyboard.W, E:Keyboard.E, R:Keyboard.R, T:Keyboard.T};
            this.createChildren();
            return;
        }// end function

        private function createChildren() : void
        {
            var _loc_1:TextFormat = null;
            this.mImgBg = new Image("png.mainbottom.skillbg");
            addChild(this.mImgBg);
            this.mSkillImg = new Image();
            this.mSkillImg.smoothing = true;
            this.mSkillImg.setSize(36, 36);
            this.mSkillImg.x = 3;
            this.mSkillImg.y = 3;
            this.mSkillImg.visible = false;
            this.mSkillImg.mouseEnabled = false;
            addChild(this.mSkillImg);
            this.mItemMask = new Sprite();
            this.mItemMask.graphics.clear();
            this.mItemMask.graphics.beginFill(0, 0.7);
            this.mItemMask.graphics.drawRect(3, 3, 36, 36);
            this.mItemMask.visible = false;
            this.mItemMask.mouseEnabled = false;
            addChild(this.mItemMask);
            _loc_1 = new TextFormat();
            _loc_1.font = "宋体";
            _loc_1.color = 16777215;
            _loc_1.align = TextFormatAlign.RIGHT;
            this.mTextNum = new TextField();
            this.mTextNum.defaultTextFormat = _loc_1;
            this.mTextNum.width = 36;
            this.mTextNum.height = 14;
            this.mTextNum.x = 3;
            this.mTextNum.y = 4;
            this.mTextNum.visible = false;
            this.mTextNum.mouseEnabled = false;
            addChild(this.mTextNum);
            this.mImgLabel = new Image("jpg.mainbottom.skilllabel");
            this.mImgLabel.x = 12;
            this.mImgLabel.y = 31;
            this.mImgLabel.mouseEnabled = false;
            addChild(this.mImgLabel);
            _loc_1.align = TextFormatAlign.CENTER;
            this.mTextSC = new TextField();
            this.mTextSC.defaultTextFormat = _loc_1;
            this.mTextSC.width = 43;
            this.mTextSC.height = 16;
            this.mTextSC.y = 31;
            this.mTextSC.mouseEnabled = false;
            addChild(this.mTextSC);
            this.mImgBg.addEventListener(MouseEvent.ROLL_OVER, this.rollOverHandler);
            addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDownHandler);
            addEventListener(MouseEvent.MOUSE_UP, this.mouseUpHandler);
            addEventListener(MouseEvent.CLICK, this.clickHandler);
            return;
        }// end function

        protected function mouseUpHandler(event:MouseEvent) : void
        {
            if (this.timeoutID)
            {
                clearTimeout(this.timeoutID);
                this.timeoutID = 0;
            }
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = getTimer();
            if (_loc_2 - this.mClickTime < 300)
            {
                this.doubleClick();
                this.mClickTime = 0;
            }
            else
            {
                this.mClickTime = _loc_2;
            }
            return;
        }// end function

        protected function doubleClick() : void
        {
            if (this.timeoutID)
            {
                clearTimeout(this.timeoutID);
                this.timeoutID = 0;
            }
            dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, false, 0, this.SHORTObj[this.shortcut]));
            return;
        }// end function

        private function rollOverHandler(event:MouseEvent) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            this.mImgBg.removeEventListener(MouseEvent.ROLL_OVER, this.rollOverHandler);
            var _loc_2:* = new Point(stage.mouseX, stage.mouseY);
            if (this.mData)
            {
                ModelProxy.change("tips", "state", "close");
                _loc_3 = this.mData;
                if (this.mType == this.SKILL_TYPE)
                {
                    _loc_4 = new Object();
                    _loc_4 = {cfg:this.mData, plus:new Object()};
                    ModelProxy.change("tips", "skill", {data:_loc_4, p:_loc_2});
                }
                else
                {
                    ModelProxy.change("tips", "tool", {data:_loc_3, p:_loc_2});
                }
            }
            else
            {
                ModelProxy.change("tips", "normal", {data:"可以打开人物面板放入技能\n可以打开背包面板放入道具", p:_loc_2});
            }
            this.mImgBg.addEventListener(MouseEvent.ROLL_OUT, this.rollOutHandler);
            return;
        }// end function

        private function rollOutHandler(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "state", "close");
            this.mImgBg.addEventListener(MouseEvent.ROLL_OVER, this.rollOverHandler);
            return;
        }// end function

        private function mouseDownHandler(event:MouseEvent) : void
        {
            if (this.timeoutID > 0)
            {
                clearTimeout(this.timeoutID);
                this.timeoutID = 0;
            }
            this.timeoutID = setTimeout(this.doDig, 200);
            return;
        }// end function

        private function doDig() : void
        {
            if (this.timeoutID > 0)
            {
                clearTimeout(this.timeoutID);
                this.timeoutID = 0;
            }
            if (!this.mData)
            {
                return;
            }
            var _loc_1:* = this.mSkillImg.getRect(this.mSkillImg);
            var _loc_2:* = new BitmapData(_loc_1.width, _loc_1.height, true, 0);
            _loc_2.draw(this.mSkillImg);
            AppDragMgr.clingyItem(this, this.mSkillInfo, new Bitmap(_loc_2), AppDragMgr.DRAG);
            return;
        }// end function

        public function setInfo(param1:int, param2:int) : void
        {
            var _loc_4:Object = null;
            var _loc_5:int = 0;
            var _loc_6:Number = NaN;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_3:Object = null;
            if (param1 > 0)
            {
                if (param2 == this.SKILL_TYPE)
                {
                    _loc_3 = this.cfgSkills[param1];
                }
                else if (param2 == this.ITEM_TYPE)
                {
                    _loc_3 = this.cfgItems[param1];
                }
            }
            if (_loc_3 == this.mData)
            {
                this.checkAuto();
                return;
            }
            this.mData = _loc_3;
            this.mID = param1;
            this.mType = param2;
            this.update();
            if (param2 == 1)
            {
                _loc_4 = UserData.getUserData("skills");
                if (_loc_4)
                {
                    _loc_5 = getTimer();
                    _loc_6 = ModelProxy.getValue("data", "servertime") + _loc_5;
                    _loc_7 = 0;
                    if (_loc_4[param1] && _loc_4[param1].cdtime > 0)
                    {
                        _loc_7 = Math.max(0, _loc_4[param1].cdtime - _loc_6);
                        if (_loc_7 > 0)
                        {
                            this.mShowingCD = true;
                            this.mCDTime = int(this.data.sleeptime);
                            this.mStartCDTime = _loc_5 - (int(this.data.sleeptime) - 1) * 1000 + _loc_7;
                            _loc_8 = this.getCurCd(_loc_5);
                            this.startCD(_loc_8);
                        }
                    }
                }
            }
            this.checkAuto();
            return;
        }// end function

        public function cancel() : void
        {
            this.stopCD();
            this.mID = 0;
            this.mType = 0;
            this.mData = null;
            this.update();
            this.checkAuto();
            return;
        }// end function

        public function startOwnCD(param1:int = 0) : void
        {
            if (this.mShowingCD && param1 == 0 || !this.mData)
            {
                return;
            }
            this.mShowingCD = true;
            var _loc_2:* = this.attSpeed;
            if (this.mType == this.SKILL_TYPE)
            {
                if (this.mData.sleeptime > 0)
                {
                    if (param1 == 0)
                    {
                        this.mCDTime = int(this.data.sleeptime);
                    }
                    else
                    {
                        this.mCDTime = int(this.data.sleeptime);
                        this.mStartCDTime = getTimer() - (int(this.data.sleeptime) - param1 - 1) * 1000;
                    }
                }
                else
                {
                    this.mCDTime = 100 / _loc_2;
                }
            }
            else if (this.mData.usecd > 0)
            {
                this.mCDTime = int(this.data.usecd);
            }
            else
            {
                this.mCDTime = 100 / _loc_2;
            }
            this.startCD(param1);
            return;
        }// end function

        public function startShareCD() : void
        {
            if (this.mShowingCD || !this.mData)
            {
                return;
            }
            var _loc_1:* = this.attSpeed;
            this.mCDTime = 100 / _loc_1;
            this.startCD();
            return;
        }// end function

        private function startCD(param1:int = 0) : void
        {
            if (param1 == 0)
            {
                this.mStartCDTime = getTimer();
            }
            if (this.mIntervalID > 0)
            {
                clearInterval(this.mIntervalID);
                this.mIntervalID = 0;
            }
            this.mIntervalID = setInterval(this.cdProgress, 1000 / 30);
            return;
        }// end function

        public function getCurCd(param1:int) : int
        {
            return Math.max(this.mCDTime - (param1 - this.mStartCDTime) / 1000, 0);
        }// end function

        private function cdProgress() : void
        {
            if (!this.mWedgeSprite)
            {
                this.mWedgeSprite = new Sprite();
            }
            this.mWedgeSprite.x = this.mSkillImg.x;
            this.mWedgeSprite.y = this.mSkillImg.y;
            this.mWedgeSprite.mouseEnabled = false;
            addChildAt(this.mWedgeSprite, 2);
            if (!this.mMaskSprite)
            {
                this.mMaskSprite = new Sprite();
                this.mMaskSprite.graphics.clear();
                this.mMaskSprite.graphics.beginFill(0, 1);
                this.mMaskSprite.graphics.drawRect(this.mSkillImg.x, this.mSkillImg.y, this.mSkillImg.width, this.mSkillImg.height);
                addChild(this.mMaskSprite);
                this.mWedgeSprite.mask = this.mMaskSprite;
            }
            if (!this.mMaskSprite.visible)
            {
                this.mMaskSprite.visible = true;
            }
            var _loc_1:* = getTimer();
            var _loc_2:* = 360 * (_loc_1 - this.mStartCDTime) / (this.mCDTime * 1000);
            if (_loc_2 >= 360)
            {
                this.stopCD();
                return;
            }
            this.mWedgeSprite.graphics.clear();
            this.mWedgeSprite.graphics.lineStyle(0, 0, 0);
            this.mWedgeSprite.graphics.beginFill(0, 0.5);
            DrawUtils.wedge(this.mWedgeSprite, this.mSkillImg.width / 2, this.mSkillImg.height / 2, 90, 360 - _loc_2, this.mSkillImg.width, this.mSkillImg.height);
            return;
        }// end function

        public function stopCD() : void
        {
            if (this.mIntervalID > 0)
            {
                clearInterval(this.mIntervalID);
                this.mIntervalID = 0;
            }
            this.mShowingCD = false;
            this.mCDTime = 0;
            if (this.mWedgeSprite)
            {
                if (this.mWedgeSprite.parent)
                {
                    this.mWedgeSprite.parent.removeChild(this.mWedgeSprite);
                }
            }
            if (this.mMaskSprite)
            {
                this.mMaskSprite.visible = false;
            }
            return;
        }// end function

        public function updateItemNum() : void
        {
            var _loc_1:int = 0;
            if (this.mType == this.ITEM_TYPE && this.mData)
            {
                _loc_1 = this.getItemNum(this.mData.itemid);
                this.mTextNum.text = _loc_1.toString();
                this.mItemMask.visible = _loc_1 <= 0;
            }
            return;
        }// end function

        public function changeSkillEffect(param1:Boolean) : void
        {
            return;
        }// end function

        private function update() : void
        {
            var _loc_1:String = null;
            var _loc_2:Object = null;
            var _loc_3:Boolean = false;
            var _loc_4:Object = null;
            var _loc_5:int = 0;
            if (this.mData)
            {
                if (this.mType == this.SKILL_TYPE)
                {
                    _loc_1 = StringUtil.substitute("{0}{1}.jpg", UIGlobel.getItemd(3), this.mData.icon);
                    this.mTextNum.visible = false;
                    this.mItemMask.visible = false;
                }
                else
                {
                    _loc_1 = StringUtil.substitute("{0}{1}{2}", UIGlobel.getItemd(1), this.mData.icon, Config.IconExtension);
                    _loc_5 = this.getItemNum(this.mData.itemid);
                    this.mTextNum.text = _loc_5.toString();
                    this.mItemMask.visible = _loc_5 <= 0;
                }
                this.mSkillImg.url = Config.getUrl(_loc_1, ResourceType.NORMAL);
                this.mSkillImg.visible = true;
                _loc_2 = UserData.getUserData("skills");
                _loc_3 = false;
                for each (_loc_4 in _loc_2)
                {
                    
                    if (_loc_4.hasOwnProperty("skillid") && _loc_4.skillid == this.id)
                    {
                        if (int(_loc_4["switch"]) > 0 && _loc_4.on == 1)
                        {
                            _loc_3 = true;
                        }
                        break;
                    }
                }
                this.changeSkillEffect(_loc_3);
            }
            else
            {
                this.mSkillImg.visible = false;
                this.mTextNum.visible = false;
                this.mItemMask.visible = false;
                this.changeSkillEffect(false);
            }
            this.startCD();
            return;
        }// end function

        private function getItemNum(param1:int) : int
        {
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_6:int = 0;
            var _loc_2:* = UserData.getBagData();
            var _loc_3:int = 0;
            for (_loc_4 in _loc_2)
            {
                
                _loc_5 = _loc_2[_loc_4];
                if (_loc_5 && _loc_5.hasOwnProperty("4"))
                {
                    if (_loc_5[2] == 2)
                    {
                        _loc_6 = _loc_5[1];
                        _loc_5 = _loc_5[4];
                        if (_loc_5 != null && _loc_5.hasOwnProperty("itemid"))
                        {
                            if (_loc_5.itemid == param1)
                            {
                                _loc_3 = _loc_3 + _loc_6;
                            }
                        }
                    }
                }
            }
            return _loc_3;
        }// end function

        public function set cdTime(param1:int) : void
        {
            this.mCDTime = param1;
            return;
        }// end function

        public function get cdTime() : int
        {
            return this.mCDTime;
        }// end function

        public function get data() : Object
        {
            return this.mData;
        }// end function

        public function get id() : int
        {
            return this.mID;
        }// end function

        public function get type() : int
        {
            return this.mType;
        }// end function

        public function set shortcut(param1:String) : void
        {
            if (this.mTextSC)
            {
                this.mTextSC.text = param1;
            }
            this.mShortcut = param1;
            return;
        }// end function

        public function get shortcut() : String
        {
            return this.mShortcut;
        }// end function

        private function get cfgSkills() : Object
        {
            return ConfigDictionary.data.skill;
        }// end function

        private function get cfgItems() : Object
        {
            return ConfigDictionary.data.item;
        }// end function

        private function get attSpeed() : int
        {
            var _loc_1:* = UserData.getUserData();
            if (_loc_1 == null)
            {
                return 1000;
            }
            if (_loc_1.abilityBuf)
            {
                return int(_loc_1.ability.attackSpeed) + int(_loc_1.abilityBuf.attackSpeed);
            }
            return int(_loc_1.ability.attackSpeed);
        }// end function

        public function setAuto() : void
        {
            if (!this.mAutoSkillMC)
            {
            }
            return;
        }// end function

        public function checkAuto() : void
        {
            var _loc_1:Object = null;
            if (this.mType == 1 && this.mData && (this.mData.groupid == 10600 || this.mData.groupid == 10900))
            {
                if (this.mData.groupid == 10600)
                {
                    _loc_1 = ModelProxy.getValue("data", "option");
                    if (_loc_1.battleoption1 == 1)
                    {
                        this.setAuto();
                        return;
                    }
                }
                else if (this.mData.groupid == 10900)
                {
                    _loc_1 = ModelProxy.getValue("data", "option");
                    if (_loc_1.battleoption2 == 1)
                    {
                        this.setAuto();
                        return;
                    }
                }
            }
            if (this.mAutoSkillMC)
            {
                if (this.mAutoSkillMC.parent)
                {
                    this.mAutoSkillMC.parent.removeChild(this.mAutoSkillMC);
                }
                this.mAutoSkillMC.dispose();
                this.mAutoSkillMC = null;
            }
            return;
        }// end function

        private function loadAutoSkill(param1) : void
        {
            var _loc_2:Object = null;
            if (this.mType == 1 && (this.mData.groupid == 10600 || this.mData.groupid == 10900))
            {
                if (this.mData.groupid == 10600)
                {
                    _loc_2 = ModelProxy.getValue("data", "option");
                    if (_loc_2.battleoption1 == 1)
                    {
                        this.addAutoSkillMovie();
                        return;
                    }
                }
                else if (this.mData.groupid == 10900)
                {
                    _loc_2 = ModelProxy.getValue("data", "option");
                    if (_loc_2.battleoption2 == 1)
                    {
                        this.addAutoSkillMovie();
                        return;
                    }
                }
            }
            if (this.mAutoSkillMC)
            {
                if (this.mAutoSkillMC.parent)
                {
                    this.mAutoSkillMC.parent.removeChild(this.mAutoSkillMC);
                }
                this.mAutoSkillMC.dispose();
                this.mAutoSkillMC = null;
            }
            return;
        }// end function

        private function addAutoSkillMovie() : void
        {
            var _loc_1:XML = null;
            if (!this.mAutoSkillMC)
            {
                _loc_1 = new XML(new effect2Xml());
                this.mAutoSkillMC = new XMLMovieClip("png.movie.autoskill", _loc_1);
            }
            if (!this.mAutoSkillMC.parent)
            {
                this.mAutoSkillMC.interval = 100;
                this.mAutoSkillMC.x = -78;
                this.mAutoSkillMC.y = -78;
                this.mAutoSkillMC.play();
                this.mAutoSkillMC.blendMode = BlendMode.ADD;
                this.mAutoSkillMC.mouseEnabled = false;
                addChildAt(this.mAutoSkillMC, 2);
            }
            return;
        }// end function

    }
}
